/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.BusinessBillBean;
import com.minierp.bean.BusinessBillItemBean;
import com.minierp.bean.ClieUserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.MakeTypeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.NotAllowException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.BusinessBillItem;
import com.minierp.model.CodeRule;
import com.minierp.model.EnterpriseInfo;
import com.minierp.service.BusinessBillService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class BusiOnlineFormCtr extends SimpleFormController {
	private BusinessBillItemDao businessBillItemDao;
	private BusinessBillDao businessBillDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private AccessoryDao accessoryDao;
	private MakeTypeDao makeTypeDao;
	private IdGenerator idGenerator;
	private BusinessBillService businessBillService;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		refData.put("mtList", this.makeTypeDao.getList(false));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		ClieUserBean ub = EncCommonHelper.getClieUserAccount(request);
		BusinessBillBean form = new BusinessBillBean();
		BusinessBill bill = new BusinessBill();
		bill.setAddress(ub.getClie().getAddress());
		bill.setLinkman(ub.getClie().getLinkman());
		bill.setPhone(ub.getClie().getLinkmanPhone());
		form.setBusinessBill(bill);
		form.setBusinessBillExt(new BusinessBillExt());
		String tmpCode = ub.getUser().getId() + "_"
				+ System.currentTimeMillis();
		form.setTmpCode(tmpCode);
		Date now = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		c.add(5, 1);
		form.setDeliverDateStr(sf.format(c.getTime()));
		return form;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		BusinessBillBean form = (BusinessBillBean) command;
		int num = Integer.parseInt(request.getParameter("item_num"));
		ArrayList arr = new ArrayList();

		for (int i = 0; i < num; ++i) {
			BusinessBillItemBean cb = new BusinessBillItemBean();
			BusinessBillItem c = new BusinessBillItem();
			c.setPriceType(request.getParameter("pt_" + i));
			c.setCost(new BigDecimal(request.getParameter("cost_" + i)));
			c.setMakeRequire(request.getParameter("mr_" + i));
			c.setFinishedContent(request.getParameter("cont_" + i));
			c.setSpecification(request.getParameter("spec_" + i));
			c.setLengType(request.getParameter("lt_" + i));
			c.setUnit(request.getParameter("unit_" + i));
			c.setProId(Long.parseLong(request.getParameter("prod_id_" + i)));
			c.setProName(request.getParameter("prod_name_" + i));
			c.setMakeTypeId(Long.parseLong(request.getParameter("mt_id_" + i)));
			c.setWcId(Long.parseLong(request.getParameter("wc_id_" + i)));
			c.setSquareNum(new BigDecimal(request.getParameter("squa_" + i)));
			c.setAmount(new BigDecimal(request.getParameter("amou_" + i)));
			c.setLength(new BigDecimal(request.getParameter("leng_" + i)));
			c.setWidth(new BigDecimal(request.getParameter("widt_" + i)));
			c.setUnitPrice(new BigDecimal(request.getParameter("up_" + i)));
			cb.setAmountStr(request.getParameter("amou_" + i));
			cb.setUnitPriceStr(request.getParameter("up_" + i));
			cb.setLengthStr(request.getParameter("leng_" + i));
			cb.setWidthStr(request.getParameter("widt_" + i));
			cb.setStatus(request.getParameter("stat_" + i));
			cb.setAcceId(Long.parseLong(request.getParameter("acce_id_" + i)));
			cb.setBusinessBillItem(c);
			arr.add(cb);
		}

		form.setItems(arr);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String alertMsg = "";
		BusinessBillBean form = (BusinessBillBean) command;
		BusinessBill obj = form.getBusinessBill();
		ClieUserBean ub = EncCommonHelper.getClieUserAccount(request);
		EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
		CodeRule cr = this.codeRuleDao.get("BUSINESS_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for businessBill");
		} else {
			try {
				this.businessBillService.txInsertForOnline(form, cr, ent, ub,
						this.businessBillDao, this.businessBillItemDao,
						this.accessoryDao, this.serialNumberDao,
						this.idGenerator);
			} catch (CannotAcquireLockException arg12) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg13) {
				EncCommonHelper.setAlertMessage(request, arg13.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "增加成功！";
			EncCommonHelper.setAlertMessage(request, alertMsg);
			mv.addObject("returnValue", String.valueOf(obj.getId()));
			return mv;
		}
	}

	public MakeTypeDao getMakeTypeDao() {
		return this.makeTypeDao;
	}

	public void setMakeTypeDao(MakeTypeDao makeTypeDao) {
		this.makeTypeDao = makeTypeDao;
	}

	public BusinessBillItemDao getBusinessBillItemDao() {
		return this.businessBillItemDao;
	}

	public void setBusinessBillItemDao(BusinessBillItemDao businessBillItemDao) {
		this.businessBillItemDao = businessBillItemDao;
	}

	public BusinessBillDao getBusinessBillDao() {
		return this.businessBillDao;
	}

	public void setBusinessBillDao(BusinessBillDao businessBillDao) {
		this.businessBillDao = businessBillDao;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public AccessoryDao getAccessoryDao() {
		return this.accessoryDao;
	}

	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public BusinessBillService getBusinessBillService() {
		return this.businessBillService;
	}

	public void setBusinessBillService(BusinessBillService businessBillService) {
		this.businessBillService = businessBillService;
	}
}